﻿
namespace Service
{
    using Data.Repository;
    using System.Linq;
	using System.Collections.Generic;

    public class ProductService : IProductService
    {
        private IProductRepository _productRepository;

        #region IProductService Members

        /// <summary>
        /// create new product
        /// </summary>
        /// <param name="theNewProduct">the new product</param>
        /// <returns>the new product's ID</returns>
        public int CreateNewProduct(Data.Entity.Product theNewProduct)
        {
            if (theNewProduct == null)
            {
                return 0;
            }

            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.CreateNewProduct(theNewProduct);
        }

        public int DeleteProduct(int id)
        {
            if (id <= 0)
            {
                return 0;
            }

            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.DeleteProduct(id);
        }

		public IList<Data.Entity.Product> GetProductsByCategory(int categoryID)
        {
            if (categoryID <= 0)
            {
                return null;
            }

            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.GetProductsByCategory(categoryID);
        }

        public IList<Data.Entity.Product> GetProductsByTag(int tagID)
        {
            if (tagID <= 0)
            {
                return null;
            }

            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.GetProductsByTag(tagID);

        }

        public Data.Entity.Product GetProduct(int productID)
        {
            if (productID <= 0)
            {
                return null;
            }

            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.GetProduct(productID);
        }

        public int UpdateProduct(Data.Entity.Product theProduct)
        {
            if (theProduct == null || theProduct.ProductID <= 0)
            {
                return 0;
            }

            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.UpdateProduct(theProduct);
        }

        public IList<Data.Entity.Category> GetTheProductCategories(int productID)
        {
            if (productID <= 0)
            {
                return null;
            }

            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.GetTheProductCategories(productID);
        }

		public IList<Data.Entity.Product> GetProductByFilter(string metaKeywords, string noMetaKeywords, int categoryID, decimal startPrice, decimal endPrice, int languageID)
        {
            if (_productRepository == null)
            {
                _productRepository = new ProductRepository();
            }

            return _productRepository.GetProductByFilter(metaKeywords, noMetaKeywords, categoryID, startPrice, endPrice, languageID);
        }

		public IList<Data.Entity.Product> GetProductsByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetProductsByLanguage(languageID);
		}

		public IList<Data.Entity.Product> GetAllProductsByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetAllProductsByLanguage(languageID);
		}

		public IList<Data.Entity.ProductTag> GetTheProductTags(int theProductID)
		{
			if(theProductID <= 0)
			{
				return null;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetTheProductTags(theProductID);
		}

		public void SaveProduct_Category_Mapping(Data.Entity.Product_Category_Mapping theProduct_Category_Mapping)
		{
			if(theProduct_Category_Mapping == null)
			{
				return;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			_productRepository.SaveProduct_Category_Mapping(theProduct_Category_Mapping);
		}

		public void SaveProductTag_Product_Mapping(Data.Entity.ProductTag_Product_Mapping theProductTag_Product_Mapping)
		{
			if(theProductTag_Product_Mapping == null)
			{
				return;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			_productRepository.SaveProductTag_Product_Mapping(theProductTag_Product_Mapping);
		}

		public IList<Data.Entity.Product_Category_Mapping> GetProduct_Category_MappingsByProduct(int theProductID)
		{
			if(theProductID <= 0)
			{
				return null;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetProduct_Category_MappingsByProduct(theProductID);
		}

		public bool DeleteProduct_Category_Mapping(Data.Entity.Product_Category_Mapping theProduct_Category_Mapping)
		{
			if(theProduct_Category_Mapping == null || theProduct_Category_Mapping.ProductCategoryID <= 0)
			{
				return false;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.DeleteProduct_Category_Mapping(theProduct_Category_Mapping);
		}

		public IList<Data.Entity.ProductTag_Product_Mapping> GetProductTag_Product_MappingByProduct(int theProductID)
		{
			if(theProductID <= 0)
			{
				return null;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetProductTag_Product_MappingByProduct(theProductID);
		}

		public bool DeleteProductTag_Product_Mapping(Data.Entity.ProductTag_Product_Mapping theProductTag_Product_Mapping)
		{
			if(theProductTag_Product_Mapping == null)
			{
				return false;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.DeleteProductTag_Product_Mapping(theProductTag_Product_Mapping);
		}

		public int IsExistingTheProductAndTagMapping(int productID, int tagID)
		{
			if(productID <= 0 || tagID <= 0)
			{
				return -1;
			}

			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.IsExistingTheProductAndTagMapping(productID, tagID);
		}

		public IList<Data.Entity.Product> GetTopSellingProducts()
		{
			if(_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetTopSellingProducts();
		}

		public IList<Data.Entity.Product> GetProductsByCategory(int categoryID, int pageIndex, int pageSize, out int totalRecords)
		{
			if (categoryID <= 0)
			{
				totalRecords = 0;
				return null;
			}

			if (_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetProductsByCategory(categoryID, pageIndex, pageSize, out totalRecords);
		}

		public IList<Data.Entity.Product> GetProductsByLanguage(int languageID, int pageIndex, int pageSize, out int totalRecords)
		{
			if (languageID <= 0)
			{
				totalRecords = 0;
				return null;
			}

			if (_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetProductsByLanguage(languageID, pageIndex, pageSize, out totalRecords);
		}

		public IList<Data.Entity.Product> GetProductsByTag(int tagID, int pageIndex, int pageSize, out int totalRecords)
		{
			if (tagID <= 0)
			{
				totalRecords = 0;
				return null;
			}

			if (_productRepository == null)
			{
				_productRepository = new ProductRepository();
			}

			return _productRepository.GetProductsByTag(tagID, pageIndex, pageSize, out totalRecords);
		}

		#endregion
	}
}
